﻿using System;
using System.Collections;
using System.Configuration.Install;
using System.IO;
using System.ServiceProcess;
using System.Windows.Forms;
using X18.Framework.Core.Service;

namespace X18.Gather.Console
{
    public partial class Console : Form
    {
        public Console()
        {
            InitializeComponent();
        }

        private void TimeServerStatus_Tick(object sender, EventArgs e)
        {
            string ServiceName = this.ServiceName.Text;
            bool check = CheckService(ServiceName);
            if (check)
            {
                Start.Enabled = false;
                Stop.Enabled = false;
                //
                ServiceController service = new ServiceController(ServiceName);
                switch (service.Status)
                {
                    case ServiceControllerStatus.Running:
                        Stop.Enabled = true;
                        ServiceStatus.Text = "正在运行";
                        break;
                    case ServiceControllerStatus.Stopped:
                        Start.Enabled = true;
                        ServiceStatus.Text = "已停止";
                        break;
                    case ServiceControllerStatus.Paused:
                        Start.Enabled = true;
                        ServiceStatus.Text = "已暂停";
                        break;
                    case ServiceControllerStatus.StartPending:
                        ServiceStatus.Text = "正在启动";
                        break;
                    case ServiceControllerStatus.StopPending:
                        ServiceStatus.Text = "正在停止";
                        break;
                    case ServiceControllerStatus.ContinuePending:
                        ServiceStatus.Text = "正在继续";
                        break;
                    case ServiceControllerStatus.PausePending:
                        ServiceStatus.Text = "正在暂停";
                        break;
                }
                //
                Install.Enabled = false;
                UnInstall.Enabled = true;
            }
            else
            {
                ServiceStatus.Text = "未安装";
                //
                Install.Enabled = true;
                UnInstall.Enabled = false;
                Start.Enabled = false;
                Stop.Enabled = false;
            }
        }

        private bool CheckService(string serviceName)
        {
            bool rst = false;
            ServiceController[] services = ServiceController.GetServices();
            foreach (var service in services)
            {
                if (service.ServiceName.Equals(serviceName, StringComparison.OrdinalIgnoreCase))
                {
                    rst = true;
                    break;
                }
            }
            return rst;
        }

        private void Install_Click(object sender, EventArgs e)
        {
            try
            {
                // 获取当前程序运行目录
                string CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
                // 获取服务程序文件名
                string ServiceName = this.ServiceName.Text;
                string ServiceFilePath = Path.Combine(CurrentDirectory, ServiceName + ".exe");
                //
                WindowsServiceHelper.Install(ServiceFilePath);
                //
                MessageBox.Show($"服务安装完成。");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"服务安装失败: {ex.Message}");
            }
        }

        private void UnInstall_Click(object sender, EventArgs e)
        {
            try
            {
                // 获取当前程序运行目录
                string CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
                // 获取服务程序文件名
                string ServiceName = this.ServiceName.Text;
                string ServiceFilePath = Path.Combine(CurrentDirectory, ServiceName + ".exe");
                //
                WindowsServiceHelper.Uninstall(ServiceFilePath);
                //
                MessageBox.Show($"服务卸载完成。");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"服务卸载失败: {ex.Message}");
            }
        }

        private void Start_Click(object sender, EventArgs e)
        {
            string ServiceName = this.ServiceName.Text;
            //
            try
            {
                WindowsServiceHelper.Start(ServiceName);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"服务启动异常: {ex.Message}");
            }
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            string ServiceName = this.ServiceName.Text;
            //
            try
            {
                WindowsServiceHelper.Stop(ServiceName);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"服务启动异常: {ex.Message}");
            }
        }
    }
}
